//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "UnitOfMeasureConversionManager.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Experimental/Commodities/UnitOfMeasureConversion.h>
#include <gen/QL/Experimental/Commodities/CommodityType.h>
#include <gen/QL/Experimental/Commodities/UnitOfMeasure.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::CUnitOfMeasureConversionManager (boost::shared_ptr<QuantLib::UnitOfMeasureConversionManager>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasureConversionManager = NULL;
#endif
	_ppUnitOfMeasureConversionManager = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::CUnitOfMeasureConversionManager (QuantLib::UnitOfMeasureConversionManager& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phUnitOfMeasureConversionManager = NULL;
#endif
	_ppUnitOfMeasureConversionManager = new boost::shared_ptr<QuantLib::UnitOfMeasureConversionManager> (&childNative);
    
    _UnitOfMeasureConversionManagerOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::CUnitOfMeasureConversionManager (CUnitOfMeasureConversionManager^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasureConversionManager = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUnitOfMeasureConversionManager = new boost::shared_ptr<QuantLib::UnitOfMeasureConversionManager> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::CUnitOfMeasureConversionManager (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasureConversionManager = NULL;
#endif
	if (!t->IsSubclassOf(CUnitOfMeasureConversionManager::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::CUnitOfMeasureConversionManager (QuantLib::Handle<QuantLib::UnitOfMeasureConversionManager>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phUnitOfMeasureConversionManager = &childNative;
	_ppUnitOfMeasureConversionManager = &static_cast<boost::shared_ptr<QuantLib::UnitOfMeasureConversionManager>>(childNative.currentLink());
    
    _UnitOfMeasureConversionManagerOwner = owner;
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::CUnitOfMeasureConversionManager (QuantLib::Handle<QuantLib::UnitOfMeasureConversionManager> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phUnitOfMeasureConversionManager = &childNative;
	_ppUnitOfMeasureConversionManager = &static_cast<boost::shared_ptr<QuantLib::UnitOfMeasureConversionManager>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::CUnitOfMeasureConversionManager (QuantLib::UnitOfMeasureConversionManager childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasureConversionManager = NULL;
#endif
	_ppUnitOfMeasureConversionManager = new boost::shared_ptr<QuantLib::UnitOfMeasureConversionManager> (new QuantLib::UnitOfMeasureConversionManager (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::~CUnitOfMeasureConversionManager ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppUnitOfMeasureConversionManager != NULL)
    {
	    delete _ppUnitOfMeasureConversionManager;
        _ppUnitOfMeasureConversionManager = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::!CUnitOfMeasureConversionManager ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppUnitOfMeasureConversionManager != NULL)
    {
	    delete _ppUnitOfMeasureConversionManager;
    }
}
QuantLib::UnitOfMeasureConversionManager& Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::GetReference ()
{
    if (_ppUnitOfMeasureConversionManager == NULL) throw REFNEW NativeNullException ();
	return **_ppUnitOfMeasureConversionManager;
}
boost::shared_ptr<QuantLib::UnitOfMeasureConversionManager>& Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::GetShared ()
{
    if (_ppUnitOfMeasureConversionManager == NULL) throw REFNEW NativeNullException ();
	return *_ppUnitOfMeasureConversionManager;
}
QuantLib::UnitOfMeasureConversionManager* Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::GetPointer ()
{
    if (_ppUnitOfMeasureConversionManager == NULL) throw REFNEW NativeNullException ();
	return &**_ppUnitOfMeasureConversionManager;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::UnitOfMeasureConversionManager>& Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::GetHandle ()
{
	if (_phUnitOfMeasureConversionManager == NULL)
	{
		_phUnitOfMeasureConversionManager = new Handle<QuantLib::UnitOfMeasureConversionManager> (*_ppUnitOfMeasureConversionManager);
	}
	return *_phUnitOfMeasureConversionManager;
}
#endif
bool Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::HasNative () 
{
	return (_ppUnitOfMeasureConversionManager != NULL);
}

Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversionManager^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::Add (Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversion^ c)
{
    CUnitOfMeasureConversion^ _Cc;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cc = safe_cast<CUnitOfMeasureConversion^> (c);
        _Cc->Lock();
        QuantLib::UnitOfMeasureConversion& _c = static_cast<QuantLib::UnitOfMeasureConversion&> (_Cc->GetReference ()); 
    	(*_ppUnitOfMeasureConversionManager)->add ( _c );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc != nullptr) _Cc->Unlock();
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversionManager^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::Clear::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppUnitOfMeasureConversionManager)->clear ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversion^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversionManager::Lookup (Cephei::QL::Experimental::Commodities::ICommodityType^ commodityType, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ source, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ target, Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Commodities::UnitOfMeasureConversion::TypeEnum>^ type)
{
    CCommodityType^ _CcommodityType;
    CUnitOfMeasure^ _Csource;
    CUnitOfMeasure^ _Ctarget;
    try
    {
        _CcommodityType = safe_cast<CCommodityType^> (commodityType);
        _CcommodityType->Lock();
        QuantLib::CommodityType& _commodityType = static_cast<QuantLib::CommodityType&> (_CcommodityType->GetReference ()); 
        _Csource = safe_cast<CUnitOfMeasure^> (source);
        _Csource->Lock();
        QuantLib::UnitOfMeasure& _source = static_cast<QuantLib::UnitOfMeasure&> (_Csource->GetReference ()); 
        _Ctarget = safe_cast<CUnitOfMeasure^> (target);
        _Ctarget->Lock();
        QuantLib::UnitOfMeasure& _target = static_cast<QuantLib::UnitOfMeasure&> (_Ctarget->GetReference ()); 
        QuantLib::UnitOfMeasureConversion::Type _type = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Commodities::UnitOfMeasureConversion::TypeEnum>::IsSome::get (type) ? (QuantLib::UnitOfMeasureConversion::Type)type->Value : QuantLib::UnitOfMeasureConversion::Derived ); //5
    	QuantLib::UnitOfMeasureConversion _rv = (QuantLib::UnitOfMeasureConversion)(*_ppUnitOfMeasureConversionManager)->lookup ( _commodityType,  _source,  _target,  _type );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcommodityType != nullptr) _CcommodityType->Unlock();
        if (_Csource != nullptr) _Csource->Unlock();
        if (_Ctarget != nullptr) _Ctarget->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

